The Storage Kit Table of Contents | The Storage Kit Index |
Derived from: BStatable
Declared in: be/storage/Node.h
Library: libbe.so
The BNode class gives you access to the data that a file system entry (a file, directory, or symbolic link) contains. There are two parts to this data:
The content of the data portion depends on the node's flavor:
The content of the attributes, on the other hand, isn't qualified by the node's flavor: Any node can contain any set of attributes.
Keep in mind that the concept of a "node" designates the data parts (data and attributes) of a file (a file, directory, or link). Contrast this with an "entry," which designates the entity's location within the file system: For example, you can write to a "node" (but not an entry), and you can rename an "entry" (but not a node).
This isn't just a conceptual crutch, it's the law: Nodes really don't know where they're located. For example, you can't ask a node for its name, or for the identity of its parent. This has some serious implications, the most important of which is...
Now that we've got that straight, we'll relax the rules a bit:
This doesn't really change the "store the info" rule. Even if you're dealing exclusively with BDirectory objects, you should keep the generative information around. The primary reason for this is...
Every BNode object consumes a "file descriptor." Your application can only maintain 256 file descriptors at a time. Because of this limit, you shouldn't keep BNodes around that you don't need. Keep in mind that BEntry objects also consumes file descriptors (one per object).
|
BNode has three derived classes: BFile, BDirectory, and BSymLink. The derived classes define functions that let you access the node's data portion in the appropriate style; for example...
If you want to (sensibly) look at a node's data portion, you must create an instance of the appropriate derived class. In other words, if you want to browse a directory, you have to create a BDirectory instance; if you want to write to a file, you create a BFile.
Be aware that it's not (always) an error to create an instance of the "wrong" derived class; setting a BFile to a symbolic link, for example, will traverse the link such that the BFile opens the file that the symbolic link is linked to. See the individual derived class specifications for more information.
In practice, you almost always want to create an instance of one of the BNode-derived classes; but if, for whatever reason, you find yourself holding a BNode instance, here's what you'll be able to do with it:
|
There may be times when you find yourself holding on to a BNode (instance) that you want to convert into a BFile, BDirectory, or BSymLink. However, you can't go directly from a BNode instance to an instance of BFile, BDirectory, or BSymLink—you can't tell your BNode to "cast itself" as one of its children.
There are solutions, however...
Converting from a BNode to a BDirectory, while not transparent, is pretty simple: Grab the node_ref out of the BNode and pass it to the BDirectory constructor or SetTo() function. Regard this example function:
void Node2Directory(BNode *node, BDirectory *dir) { node_ref nref; if (!node || !dir) { dir.Unset(); return; } node.GetNodeRef(&nref); /* Set the BDirectory. If nref isn>t a directory node, * the SetTo() will fail. */ dir.SetTo(&nref); }
Converting a BNode instance to a BFile or BSymLink isn't as neat as the foregoing. Instead, you have to cache the information that you used to initialize the BNode in the first place, and then reuse it to create the BFile or BSymLink.
For example, let's say you receive an entry_ref. You turn it into a BNode, but then decide you need the data-writing power of a BFile. If, in the meantime, you lost the original entry_ref, you're sunk—there's nothing you can do.
Another feature provided by the BNode class is "node locking": Through BNode's Lock() function you can restrict access to the node. The lock is removed when Unlock() is called, or when the BNode object is deleted.
The one exception to the no-file descriptors rule has to do with BEntries: Let's say you lock a directory, and then you initialize a BEntry to point to an entry within that directory. Even though the BEntry creates a file descriptor to the directory (as explained in the BEntry class), the initialization will succeed.
For files (and, less importantly, symlinks), the implications of locking are pretty clear: No one else can read or write the file. For directories, it's worth a closer look:
Locking a node does not lock the node's entry: You can't "lock out" entry operations, such as rename, move, and remove. Even if you have a node locked, the entry that acts as the "container" for that node could disappear. If you want to prevent such operations on a node's entry, lock the entry's parent directory.
In general, you should try to avoid locking your nodes. If you must lock, try to make it brief. The primary reason (and, pretty much, the only reason) to lock is if separate elements in the data and/or attributes must be kept in a consistent state. In such a case, you should hold the lock just long enough to ensure consistency.
|
|
Creates a new BNode object that's initialized to represent a specific file system node. To retrieve the status of the initialization, call InitCheck() immediately after constructing the object:
BNode node("/boot/lbj/FidoOnFire.gif"); if (node.InitCheck() != B_OK) /* The object wasn>t initialized. */
A successfully initialized BNode object creates a "file descriptor" through which the object reads and writes the node's data and attributes. You can only have 256 file descriptors at a time (per application). The object's file descriptor is closed when the object is deleted, reset (through SetTo()), or unset (Unset()).
|
Frees the object's file descriptor, unlocks the node (if it was locked), and destroys the object.
|
Gets information about the attribute named by attr. The information is copied into info, which must be allocated before it's passed in.
The attr_info structure, defined in be/kernel/fs_attr.h, is
typedef struct attr_info { uint32 type; off_t size; } attr_info;
RETURN CODES
|
Every BNode maintains a pointer into its list of attributes. GetNextAttrName() retrieves the name of the attribute that the pointer is currently pointing to, and then bumps the pointer to the next attribute. The name is copied into the buffer, which should be at least B_ATTR_NAME_LENGTH characters long. The copied name is NULL-terminated. When you've asked for every name in the list, GetNextAttrName() returns an error.
|
RewindAttrs() resets the BNode's attribute pointer to the first element in the list.
To visit every attribute name, you would do something like this:
/* Print every attribute name. */ char buf[B_ATTR_NAME_LENGTH]; while (node.GetNextAttrName(buf) == B_OK) { printf("> Attr name: %sn", buf); }
The attribute list is not static; when you ask for the next attribute name, you're asking for the next name in the list as it exists right now.
Furthermore, the ordinal position of an attribute within the list is indeterminate. "Newer" attributes are not necessarily added to the end of the list: If you alter the list while you're walking through it, you may get curious results—you may not see the attribute that you just now added (for example).
In general, it's best to avoid altering the list while you're iterating over it.
RETURN CODES
|
Returns the status of the most recent initialization.
RETURN CODES
|
Locks and unlocks the BNode's node. While the node is locked, no other object can access the node's data or attributes. More precisely, no other agent can create a file descriptor to the node. If a file descriptor already exists to this node, the Lock() function fails.
See "Node Locking" for details.
RETURN CODES
|
These functions read, write, and remove the node's attributes. Attributes are name/data pairs, where names must be unique (within a given node) and the data can be of arbitrary length.
ReadAttr() reads the data in the attribute named name, and copies it in buffer. The length of the buffer (the maximum number of bytes to copy) is given by length. Currently, the type and offset arguments are unused (or unreliable). The function returns the number of bytes that were actually read.
WriteAttr() erases the data currently held by name (if such an attribute exists) and replaces it with a copy of the first length bytes of data in buffer. The type argument is remembered—you can retrieve an attribute's type through GetAttrInfo(), for example—and you need to specify the correct type when you're forming a query (see BQuery and the note below). But, as mentioned above, you don't need to match types when you're reading the attribute. The offset argument is currently unreliable and shouldn't be used. The functions returns the number of bytes that were written.
|
|
RemoveAttr() deletes the attribute given by name.
RETURN CODES
ReadAttr() and WriteAttr(), if successful, return the number of bytes read or written.
|
Moves the attribute given by name to new_name. If new_name exists, it's clobbered.
RETURN CODES
|
Closes the BNode's current file descriptor and opens it on the node (of the entry) that's designated by the arguments.
BNode instances never traverse symbolic links. If the designated entry is a symbolic link, the BNode will open the link's node. (Conversely, BFile instances always traverse symbolic links.)
Unset() closes the BNode's file descriptor and sets InitCheck() to B_NO_INIT.
RETURN CODES
|
Immediately performs any pending disk transactions for the file, returning B_OK on success and an appropriate error message otherwise.
|
In the expression
BNode a = b;
BNode a is initialized to refer to the same node as b. To gauge the success of the assignment, you should call InitCheck() immediately afterwards. It's safe to assign a BNode to itself.
|
Two BNode objects are said to be equal if they're set to the same node, or if they're both B_NO_INIT.
The Storage Kit Table of Contents | The Storage Kit Index |
Copyright © 2000 Be, Inc. All rights reserved..